package com.yz.xyt.controller;

import com.github.pagehelper.PageInfo;
import com.yz.xyt.constant.HttpStatus;
import com.yz.xyt.domain.ResponseResult;
import com.yz.xyt.domain.Staff;
import com.yz.xyt.domain.vo.StaffVo;
import com.yz.xyt.service.IBaseDataImportService;
import com.yz.xyt.service.IStaffService;
import com.yz.xyt.utils.DateUtils;
import com.yz.xyt.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 教职工账号信息管理
 */
@RestController
@RequestMapping("/system")
public class StaffManagerController {

    @Autowired
    IStaffService staffService;

    @Autowired
    IBaseDataImportService baseDataImportService;

    /**
     * 分页查询教职工信息
     * @param staffVo
     * @return
     */
    @RequestMapping("/getStaffList")
    public ResponseResult getStaffList(StaffVo staffVo){
        ResponseResult result = new ResponseResult();
        PageInfo staffPage = staffService.findStaffForPage(staffVo);
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("查询成功");
        result.setData(staffPage);
        return result;
    }

    /**
     * 教职工数据导入
     * @param file
     * @return
     */
    @RequestMapping("/importStaff")
    public ResponseResult importStaff(MultipartFile file){
        ResponseResult result = baseDataImportService.importStaff(file);
        return result;
    }

    /**
     * 辅导员列表
     * @return
     */
    @RequestMapping("/instructorList")
    public ResponseResult getInstructorList(){
        ResponseResult result = new ResponseResult();
        result.setCode(HttpStatus.SUCCESS);
        result.setMsg("查询辅导员列表成功");
        List<Staff> instructorList = staffService.getInstructorList();
        result.setData(instructorList);
        return result;
    }

    /**
     * 更新教职工信息
     * @param staff
     * @return
     */
    @RequestMapping("/editStaff")
    public ResponseResult editStaff(Staff staff){

        ResponseResult result = new ResponseResult();
        try {
            result = staffService.editStaff(staff);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("教职工信息修改失败");
            result.setCode(HttpStatus.ERROR);
        }
        return result;
    }

    /**
     * 新增教职工
     * @param staff
     * @return
     */
    @RequestMapping("/addStaff")
    public ResponseResult addStaff(Staff staff){
        ResponseResult result = new ResponseResult();

        staff.setCreateBy(SecurityUtils.getUsername());
        staff.setCreateDate(DateUtils.getNowDate());

        try {
            result = staffService.addStaff(staff);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("新增教职工失败");
            result.setCode(HttpStatus.ERROR);
        }
        return result;
    }

    /**
     * 删除教职工
     * @param staff
     * @return
     */
    @RequestMapping("/deleteStaff")
    public ResponseResult deleteStaff(Staff staff){
        ResponseResult result = new ResponseResult();
        try {
            result = staffService.deleteByStaff(staff);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("删除教职工失败");
            result.setCode(HttpStatus.ERROR);
        }
        return result;
    }

    /**
     * 查询教师信息
     * @param staffVo
     * @return
     */
    @RequestMapping("/queryStaffInfo")
    public ResponseResult queryStaffInfo(StaffVo staffVo){
        ResponseResult result = new ResponseResult();
        try {
            result = staffService.queryStaffInfo(staffVo);
        }catch (Exception e){
            e.printStackTrace();
            result.setMsg("教职工信息查询失败");
            result.setCode(HttpStatus.ERROR);
        }
        return result;
    }
}
